package com.dyz.LeetCodeforSwordFingerOffer;

import java.util.ArrayList;

public class MinArray11 {
    public int minArray(int[] numbers) {
        if(numbers == null || numbers.length==0){return -1;}
        //[1,1,1,2,1]  [3,1,3]
        //去重
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=numbers.length-1;i>=0;i--){
            if(list.contains(numbers[i])){continue;}
            list.add(numbers[i]);
        }
        //取最小
        int min = list.get(0);
        for(int i=1; i<list.size();i++){
            if(min>list.get(i)){
                min=list.get(i);
            }
        }
        return min;
    }
    //使用二分法
    public int minArray1(int[] numbers) {
        if(numbers == null || numbers.length==0){return -1;}
        int low = 0;
        int high = numbers.length-1;

        while (low<high){
            int mid = (low+high)>>1;
            //查看mid落在了哪个数组中
            if(numbers[mid] < numbers[high]){
                high = mid;//说明mid落在第二个数组中
            }else if(numbers[mid] > numbers[high]){
                low = mid+1; //说明mid落在第一个数组中
            }else {
                high-=1;
            }
        }
        return numbers[low];
    }

    public static void main(String[] args) {
        int [] numbers = new int[]{3,1,3};
        int a = new MinArray11().minArray1(numbers);
        System.out.println(a);

    }
}
